package com.pactera.madp.cp.handler.sync.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.pactera.madp.common.core.util.SpringContextHolder;
import com.pactera.madp.cp.api.dto.sync.SyncDTO;
import com.pactera.madp.cp.api.entity.sync.SyncStateEntity;
import com.pactera.madp.cp.common.annotations.SyncType;
import com.pactera.madp.cp.common.constant.Constants;
import com.pactera.madp.cp.common.enums.sync.HrServiceEnum;
import com.pactera.madp.cp.common.enums.sync.SyncTypeEnum;
import com.pactera.madp.cp.common.util.sync.SyncUtils;
import com.pactera.madp.cp.config.SyncConfig;
import com.pactera.madp.cp.context.SyncStateContext;
import com.pactera.madp.cp.handler.sync.SyncTypeHandler;
import com.pactera.madp.cp.service.sync.template.SyncBusinessService;
import com.pactera.madp.cp.service.sync.template.SyncItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.Executor;

@Slf4j
@Component
@SyncType(SyncTypeEnum.HR)
public class HrSyncHandler implements SyncTypeHandler {

    @Override
    public Boolean handler(SyncDTO dto) {
        if(!checkFile()) {
            log.error("HR同步文件未生成完毕");
            return false;
        }

        Executor executor = SpringContextHolder.getBean(Constants.EXECUTOR_PROCESS_NAME);
        HrServiceEnum[] hrEnums = HrServiceEnum.values();
        for (HrServiceEnum hrEnum : hrEnums) {
            executor.execute(() -> {
                // 执行日志信息初始化
                SyncStateEntity syncStateEntity = new SyncStateEntity();
                syncStateEntity.setSyncType(SyncTypeEnum.HR.getCode());
                syncStateEntity.setSyncSubType(hrEnum.getSubType());
                SyncStateContext.set(syncStateEntity);
                try {
                    // 执行任务
                    ((SyncItemService)SpringContextHolder.getBean(hrEnum.getServiceType())).handler();
                    // 同步数据
                    if (hrEnum.getBusinessServiceType() == null) {
                        return;
                    }
                    SyncBusinessService syncBusinessService =
                            (SyncBusinessService) SpringContextHolder.getBean(hrEnum.getBusinessServiceType());
                    if (ObjectUtil.isNotNull(syncBusinessService)) {
                        syncBusinessService.syncBusiness();
                    }
                } finally {
                    SyncStateContext.remove();
                    // 记录执行日志
                    if (syncStateEntity.isSaveLog()) {
                        log.debug("{}{}", hrEnum.getSubType(), "记录任务执行日志");
                        syncStateEntity.insert();
                        log.debug("{}{}", hrEnum.getSubType(), "日志记录成功");
                    }
                }
            });
        }
        return Boolean.TRUE;
    }

    private boolean checkFile() {
        log.debug("HR信息：check");
        DateTime offsetDay = DateUtil.offsetDay(new Date(), -1);
        String fileDate = DateUtil.format(offsetDay, Constants.DATE_FORMAT);
        SyncConfig syncConfig = SpringContextHolder.getBean(SyncConfig.class);
        String filepath = SyncUtils.buildFilePath(syncConfig.getHrPath(), Constants.FN_HROK, fileDate);
        return SyncUtils.checkFile(filepath);
    }
}
